﻿using BLL.Dal;
using BLL.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SysAdmin_Dictionary_List : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["paramType"] != null && Request["paramType"] != "")
        {
            switch (Request["paramType"])
            {
                case "QueryList": QueryList(); break;
                case "AddModel": AddModel(); break;
                case "DelModel": DelModel(); break;
                case "QueryModel": QueryModel(); break;
                case "UpdateModel": UpdateModel(); break;
                default: break;
            }
        }
    }

    /// <summary>
    /// 获取所有的分页展示数据
    /// </summary>
    void QueryList()
    {

        string strWhere = " parentKey='Root'";

        if (Request["parCode"] != null && Request["parCode"] != "")
        {
            strWhere = " parentKey='" + Request["parCode"].ToString().Trim() + "'";
        }

        int PageCount = 0;

        if (Request["page"] != null)
        {
            int.TryParse(Request["page"], out PageCount);
        }

        int RowsCount = 30;

        if (Request["rows"] != null)
        {
            int.TryParse(Request["rows"], out RowsCount);
        }

        SysDictionaryDAL dicBus = new SysDictionaryDAL();
        int recordCount = dicBus.GetRecordCount(strWhere);

        DataTable dtData = dicBus.GetListByPage(strWhere, "", (RowsCount * (PageCount - 1)), RowsCount * PageCount).Tables[0];

        object obj = new { total = recordCount, rows = dtData };

        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);

        WebHelper.HttpResponseJson(response);
    }


    /// <summary>
    /// 增加新的项
    /// </summary>
    void AddModel()
    {
        SysDictionaryDAL modelBus = new SysDictionaryDAL();
        SysDictionary model = new SysDictionary();
        model.ID = Guid.NewGuid();
        model.dicKey = ReqHelper.Get<string>("dictionary_key").ToLower();
        model.dicName = ReqHelper.Get<string>("dictionary_name");
        model.parentKey = ReqHelper.Get<string>("dictionary_par_key");
        model.indexNum = 0;
        model.isActive = false;
        if (!string.IsNullOrEmpty(Request["is_active"]) && Request["is_active"] == "on")
        {
            model.isActive = true;
        }
        object obj = new { success = true, msg = "数据操作成功" };
        try
        {
            modelBus.Add(model);
            obj = new { success = true, msg = "数据操作成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }


    /// <summary>
    /// 删除选择的项
    /// </summary>
    void DelModel()
    {
        string strCode = ReqHelper.Get<string>("dicCode");
        SysDictionaryDAL modelBus = new SysDictionaryDAL();
        object obj = new { success = modelBus.Delete(new Guid(strCode)) };
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }

    /// <summary>
    /// 查询显示项
    /// </summary>
    void QueryModel()
    {

    }

    /// <summary>
    /// 更新选择的项
    /// </summary>
    void UpdateModel()
    {
        SysDictionaryDAL modelBus = new SysDictionaryDAL();
        SysDictionary model = modelBus.GetModel(new Guid(ReqHelper.Get<string>("dictionary_edit_key")));
        model.dicKey = ReqHelper.Get<string>("dictionary_key").ToLower();
        model.dicName = ReqHelper.Get<string>("dictionary_name");
        model.isActive = false;
        if (!string.IsNullOrEmpty(Request["is_active"]) && Request["is_active"] == "on")
        {
            model.isActive = true;
        }
        object obj = new { success = true, msg = "数据操作成功" };
        try
        {
            modelBus.Update(model);
            obj = new { success = true, msg = "数据操作成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);

    }
}